package kg.aibek.projecteuler.p51;

import java.util.Arrays;

public class Problem52 {
	public void solve() {
		int d = 1;
		a: while (true) {
			d++;
			int[] dd = new int[] { d, 6 * d, 2 * d, 3 * d, 4 * d, 5 * d };
			for (int i = 0; i < dd.length; i++) {
				for (int j = i + 1; j < dd.length; j++) {
					if (!arrEqual((dd[i] + "").toCharArray(),
							(dd[j] + "").toCharArray())) {
						continue a;
					}
				}
			}
			System.out.println(dd[0]);
			break a;
		}
	}

	private boolean arrEqual(char[] ch1, char[] ch2) {
		Arrays.sort(ch1);
		Arrays.sort(ch2);

		if (ch1.length != ch2.length) {
			return false;
		}

		for (int i = 0; i < ch1.length; i++) {
			if (ch1[i] != ch2[i])
				return false;
		}

		return true;
	}

	public void solve2() {
		int d = 1386;
		System.out.println(d);
		System.out.println(2 * d);
		System.out.println(3 * d);
		System.out.println(4 * d);
		System.out.println(5 * d);
		System.out.println(6 * d);
	}

	public static void main(String[] args) {
		Problem52 p = new Problem52();
		p.solve();
	}
}
